//
//  EditUserNameViewController.m
//  FaceRecognition
//
//  Created by syun on 17/8/23.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "EditUserNameViewController.h"

@interface EditUserNameViewController ()<UITextFieldDelegate>
{
    
    UIButton *editBtn;
}

@property (nonatomic, strong) UITextField *nameField;

@end

@implementation EditUserNameViewController

- (void)loadView {
        [super loadView];

    
    [self.view setBackgroundColor:colorFromHex(@"F4F5F7")];
    
    self.navBar.nav_titleLabel.text = @"修改姓名";
   
   self.navBar.rightButton.hidden = NO;
    [self.navBar.rightButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [self.navBar.rightButton setTitle:@"完成" forState:(UIControlStateNormal)];
    [self.navBar.rightButton setTitleColor:colorFromHex(@"3b3b3b") forState:(UIControlStateNormal)];
    [self.navBar.rightButton addTarget:self action:@selector(makeSure) forControlEvents:(UIControlEventTouchUpInside)];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.nameField];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Request

- (void)makeSure {
    
    if ([[self.nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        [Utils alertViewWithMessage:@"请输入名字"];
        return;
    }
    
    if (self.nameField.text.length>8) {
        
        [Utils alertViewWithMessage:@"请将名字限制在8个字内"];
        return;
        
    }
    if ([Utils stringContainsEmoji:self.nameField.text]) {
        
        [Utils alertViewWithMessage:@"不能含有特殊字符"];
        return;
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_user_edit" forKey:@"method"];
    [dict setObject:[SYAccountTool ShareAccount].account.birthday forKey:@"birthday"];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:self.nameField.text forKey:@"username"];
    [dict setObject:[NSString stringWithFormat:@"%d",[SYAccountTool ShareAccount].account.sex] forKey:@"gender"];
    
    
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dict = (NSDictionary *)jsonObject;
        NSDictionary * personalImformation = [dict objectForKey:@"re_data"];
        
        [self saveUserDataWithResposeData:personalImformation];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (void)saveUserDataWithResposeData:(NSDictionary *)personalImformation
{
    BOOL save = [SyUserInfomation personalImformationSaveWtih:personalImformation];
    NSLog(@"save status 1 is success 0 is failture :%i",save);
    NSDictionary * userinfo = [personalImformation objectForKey:@"userinfo"];
    NSString * userID = [NSString stringWithFormat:@"%@",[userinfo objectForKey:@"userid"]];
    
    NSUserDefaults * UD = [NSUserDefaults standardUserDefaults];
    [UD setObject:userID forKey:@"userid"];
    [UD synchronize];
    
}

#pragma mark - Get

- (UITextField *)nameField {
    
    if (_nameField == nil) {
        _nameField = [[UITextField alloc]initWithFrame:CGRectMake(15, SafeAreaTopHeight, SCREENWIDTH, 44)];
        _nameField.textColor = colorFromHex(@"3b3b3b");
        _nameField.placeholder = [SYAccountTool ShareAccount].account.username;
        _nameField.delegate = self;
        _nameField.font = [UIFont systemFontOfSize:16];
        [_nameField setBackgroundColor:[UIColor whiteColor]];
        
        UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, 44)];
        [bg setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:bg];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight+44, SCREENWIDTH, 0.5)];
        [line setBackgroundColor:colorFromHex(@"979797")];
        [self.view addSubview:line];
    }
    return _nameField;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
